# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=icu
pkgname=('icu' 'icu-devel')
pkgver=65.1
pkgrel=1
pkgdesc="International Components for Unicode library"
arch=('i686' 'x86_64')
url="http://www.icu-project.org/"
license=('custom:"icu"')
depends=('gcc-libs' 'sh')
source=(http://download.icu-project.org/files/${pkgname}4c/${pkgver}/${pkgname}4c-${pkgver//./_}-src.tgz
        toolutil-Makefile-ln-force.patch
        icu-59.1-msys2.patch)
sha256sums=('53e37466b3d6d6d01ead029e3567d873a43a5d1c668ed2278e253b683136d948'
            'e85928a287a73cb24f4bd8b891bb10636cda27c09bae01fc003d8bee2dda8986'
            '8192c4460a5194fb83da4d215ad98f7d8fe1a02b9787edc45f5f2189e4136704')

prepare() {
  cd ${srcdir}/icu/source

  patch -p2 -i ${srcdir}/toolutil-Makefile-ln-force.patch
  patch -p2 -i ${srcdir}/icu-59.1-msys2.patch

  libtoolize --force --copy
  autoreconf -fi
}

build() {
  cd ${srcdir}/icu/source
  
  CC=/usr/bin/gcc \
  CXX=/usr/bin/g++ \
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    --mandir=/usr/share/man \
    --sbindir=/usr/bin
  make

  make -j1 DESTDIR=${srcdir}/dest install
  pushd ${srcdir}/dest/usr/lib
    # fix installation directory
    mv msys-icu*${pkgver%%.*}.dll ${srcdir}/dest/usr/bin/
    # remove unversioned DLL symlinks
    rm -f *.dll
    # create unversioned implib symlinks
    for l in *.dll.a; do
      #dosym ${l} /usr/lib/${l//${pkgver%%.*}}
      cp ${l} ${l//${pkgver%%.*}}
    done
  popd

  sed -e '/^ICUUC_FILE=/ s/libdir/bindir/' \
    -e 's/l${LIBICU}/l${ICUPREFIX}/g' \
    -e '/l${ICUPREFIX}/ s/${ICULIBSUFFIX_VERSION}//g' \
    -i ${srcdir}/dest/usr/bin/icu-config \
    || error "sed icu-config failed"

  # Install license
  install -Dm644 ${srcdir}/icu/license.html ${srcdir}/dest/usr/share/licenses/icu/license.html
}

check() {
  cd icu/source
  make -k check # passes all
}

package_icu() {
  depends=('gcc-libs')
  groups=('libraries')
  mkdir -p $pkgdir/usr/bin
  cp -f $srcdir/dest/usr/bin/*.dll $pkgdir/usr/bin/
}

package_icu-devel() {
  pkgdesc="ICU headers and libraries"
  groups=('development')
  depends=("icu=${pkgver}")

  cp -rf $srcdir/dest/usr $pkgdir/
  rm -f $pkgdir/usr/bin/*.dll
}
